﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Telerik.Web.Mvc;
using MyVoteMVC.Service;
using MyVoteMVC.Common;
using MyVoteMVC.Web.Extensions;
using MyVoteMVC.Domain;
using MyVoteMVC.Web.Models;
using System.Web.Security;

namespace MyVoteMVC.Web.Controllers
{
    public class GridController : Controller
    {
        //
        // GET: /Grid/
        [GridAction]
        public ActionResult _ActivityAjaxBinding()
        {
            using (IActivityService pService = Factory.GetInstance<IActivityService>())
            {
                ActivityConverter pConverter = new ActivityConverter();
                var pList = pService.GetActivitiesList(DateTime.Now);
                var pResult = pList.ToModelList<VT_Activities, Activity>(pConverter);
                foreach (var pCR in pResult)
                {
                    pCR.CurrentState = "活动进行中";
                }

                var pHistoryList = pService.GetHistoryActivitiesList(DateTime.Now);
                var pHistoryResult = pHistoryList.ToModelList<VT_Activities, Activity>(pConverter);
                foreach (var pHR in pHistoryResult)
                {
                    pHR.CurrentState = "活动已结束";
                    pResult.Add(pHR);
                }
                return View(new GridModel(pResult));
            }
        }

        [GridAction]
        public ActionResult _SelectBatchEditing(string id)
        {
            //读取用户身份
            FormsIdentity pIdentity = (FormsIdentity)HttpContext.User.Identity;
            FormsAuthenticationTicket pTicket = pIdentity.Ticket;
            
            using (IQuestionService pService = Factory.GetInstance<IQuestionService>())
            {
                ProjectItemConverter pConverter = new ProjectItemConverter();
                var pQuestion = pService.GetQuestion(id);
                var pList = pQuestion.Items;
                var pResult = pList.ToModelList<VT_Items, ProjectItem>(pConverter);

                using (ITickService pTickService = Factory.GetInstance<ITickService>())
                {
                    string pTicketContent = pTickService.GetContent(pQuestion.Activity_ID,pTicket.UserData);
                    foreach (string pContent in pTicketContent.Split('&'))
                    {
                        if (pResult.Any(o=>o.ID==pContent.Split('=')[1]))
                        {
                            pResult.FirstOrDefault(o => o.ID == pContent.Split('=')[1]).Selected = true;
                        }
                    }
                }
                return View(new GridModel(pResult));
            }
        }

    }
}
